﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;

namespace MusicSearch
{
    public partial class Form_Main : Form
    {
        private List<object> list = new List<object>();
        public Form_Main()
        {
            InitializeComponent();
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            string key = this.tb_key.Text.Trim();
            if(key.Equals("")){
                return;
            }

            this.list = BaiduMusicSearch.Search(key);
            dataGridView_musics.DataSource = this.list;
            //MessageBox.Show(result);
        }

        private void dataGridView_musics_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0 || e.RowIndex > this.list.Count - 1)
            {
                return;
            }

            JObject music = (JObject)list[e.RowIndex];
            JObject detail = BaiduMusicSearch.GetInfo(music);
            FormDetail formDetail = new FormDetail(detail);
            formDetail.Show(this);
        }

        private void Form_Main_Load(object sender, EventArgs e)
        {
            
        }

        private void play_Click(object sender, EventArgs e)
        {
            if (this.dataGridView_musics.CurrentRow == null) { return; }

            int index = this.dataGridView_musics.CurrentRow.Index;

            if (index < 0 || index > this.list.Count - 1)
            {
                return;
            }

            JObject music = (JObject)list[index];
            JObject detail = BaiduMusicSearch.GetInfo(music);
            new FormPlayer(detail).Show();
        }
    }
}
